package com.luo.chengrui.module.sa.controller.admin.person;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.person.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.person.PersonDO;
import com.luo.chengrui.module.sa.convert.person.PersonConvert;
import com.luo.chengrui.module.sa.service.person.PersonService;

@Tag(name = "管理后台 - 人员 ")
@RestController
@RequestMapping("/sa/person")
@Validated
public class PersonController {

    @Resource
    private PersonService personService;

    @PostMapping("/create")
    @Operation(summary = "创建人员 ")
    @PreAuthorize("@ss.hasPermission('sa:person:create')")
    public CommonResult<String> createPerson(@Valid @RequestBody PersonCreateReqVO createReqVO) {
        return success(personService.createPerson(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新人员 ")
    @PreAuthorize("@ss.hasPermission('sa:person:update')")
    public CommonResult<Boolean> updatePerson(@Valid @RequestBody PersonUpdateReqVO updateReqVO) {
        personService.updatePerson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除人员 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:person:delete')")
    public CommonResult<Boolean> deletePerson(@RequestParam("id") String id) {
        personService.deletePerson(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得人员 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:person:query')")
    public CommonResult<PersonRespVO> getPerson(@RequestParam("id") String id) {
        PersonDO person = personService.getPerson(id);
        return success(PersonConvert.INSTANCE.convert(person));
    }

    @GetMapping("/list")
    @Operation(summary = "获得人员 列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:person:query')")
    public CommonResult<List<PersonRespVO>> getPersonList(@RequestParam("ids") Collection<String> ids) {
        List<PersonDO> list = personService.getPersonList(ids);
        return success(PersonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得人员 分页")
    @PreAuthorize("@ss.hasPermission('sa:person:query')")
    public CommonResult<PageResult<PersonRespVO>> getPersonPage(@Valid PersonPageReqVO pageVO) {
        PageResult<PersonDO> pageResult = personService.getPersonPage(pageVO);
        return success(PersonConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出人员  Excel")
    @PreAuthorize("@ss.hasPermission('sa:person:export')")
    @OperateLog(type = EXPORT)
    public void exportPersonExcel(@Valid PersonExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PersonDO> list = personService.getPersonList(exportReqVO);
        // 导出 Excel
        List<PersonExcelVO> datas = PersonConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "人员 .xls", "数据", PersonExcelVO.class, datas);
    }

}
